const express = require("express");
const app = express();
const port = 3010; // 服务端口

const jwt = require("jsonwebtoken");
const JWT_SECRET_KEY = "JWT_SECRET_KEY";
const Response = require("./utils/Response.js");

// 配置跨域
const cors = require("cors");
app.use(
  cors({
    origin: "*",
  })
);

// 解析post请求参数
app.use(express.urlencoded());

// 自定义token全局验证中间件
// const tokenTools = function(req,resp,next){
//   let token = req.headers["authorization"]
//   try{
//     let payload = jwt.verify(token,JWT_SECRET_KEY)
//     req.tokenPayload = payload
//   }catch(error){
//     resp.send(Response.error(401,'用户验证失败,请重新登录'))
//     return
//   }
//   next()
// }
// app.use(tokenTools)

// 引入外部路由
app.use(require("./router/user_info.js"))
app.use(require("./router/login.js"))
app.use(require("./router/ticket.js"))
app.use(require("./router/pay.js"))
app.use(express.static('public'))
app.get("/", (req, resp) => {
  resp.send("Hello World!")
})

app.listen(port, () => {
  console.log('春游记后端服务已启动....');
})